package au.org.uptick.mygwtpapp.client.presenter;

import au.org.uptick.mygwtpapp.shared.rpc.SendTextToServer;
import au.org.uptick.mygwtpapp.shared.rpc.SendTextToServerResult;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.*;

public class ResponsePresenter extends Presenter<ResponsePresenter.MyView, ResponsePresenter.MyProxy> {

    public static final String nameToken = "response";
    public static final String textToServerParam = "textToServer";
    private String textToServer;

    private final PlaceManager placeManager;
    private final DispatchAsync dispatcher;

    @ProxyCodeSplit
    @NameToken(nameToken)
    public interface MyProxy extends Proxy<ResponsePresenter>, Place {
    }

    public interface MyView extends View {
        Button getCloseButton();

        void setServerResponse(String serverResponse);

        void setTextToServer(String textToServer);
    }

    @Inject
    public ResponsePresenter(EventBus eventBus, MyView view, MyProxy proxy,
                             PlaceManager placeManager, DispatchAsync dispatcher) {
        super(eventBus, view, proxy);
        this.placeManager = placeManager;
        this.dispatcher = dispatcher;
    }

    @Override
    public void prepareFromRequest(PlaceRequest request) {
        super.prepareFromRequest(request);
        textToServer = request.getParameter(textToServerParam, null);
    }

    @Override
    protected void onBind() {
        super.onBind();
        registerHandler(getView().getCloseButton().addClickHandler(
                new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        placeManager.revealPlace(new PlaceRequest(
                                MainPagePresenter.nameToken));
                    }
                }));
    }

    @Override
    protected void onReset() {
        super.onReset();
        getView().setTextToServer(textToServer);
        getView().setServerResponse("Waiting for response...");
        dispatcher.execute(new SendTextToServer(textToServer),
                           new AsyncCallback<SendTextToServerResult>() {
                               @Override
                               public void onFailure(Throwable caught) {
                                   getView().setServerResponse(
                                           "An error occured: " + caught.getMessage());
                               }

                               @Override
                               public void onSuccess(SendTextToServerResult result) {
                                   getView().setServerResponse(result.getResponse());
                               }
                           });
    }

    @Override
    protected void revealInParent() {
        RevealRootContentEvent.fire(this, this);
    }
}
